﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reflection;

namespace UFSControl
{
    static class Program
    {
        /// <summary>
        /// Der Haupteinstiegspunkt für die Anwendung.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new Form1());
            //prüfen ob das Programm schon läuft
            if (Program.AlreadyRunning())
            {
                Application.Exit();
                return;
            }
            // Instead of running a form, we run an ApplicationContext.
            Application.Run(new TaskTrayApplicationContext());
        }

        /// <summary>
        /// Prüft ob die Anwendung bereits ausgeführt wird.
        /// </summary>
        /// <returns>
        /// <c>true</c> wenn die Anwendung bereits läuft,
        /// anderenfalls <c>false</c>.
        /// </returns>
        /// <remarks>n/a</remarks>
        private static bool AlreadyRunning()
        {
            Process current = Process.GetCurrentProcess();
            Process[] processes = Process.GetProcessesByName(current.ProcessName);
            foreach (Process process in processes)
            {
                if (process.Id != current.Id)
                {
                    if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
                    {
                        return true;
                    }
                }
            }
            return false;
        }
    } 
}
